home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 051-075 / disk_053 / arp / chmod / chmod.asm < prev    next >
Assembly Source File  |  1992-05-06  |  9KB  |  265 lines

  1. ;
  2. ; chmod.asm
  3. ; Written: Jan/14/87 by SDB
  4. ; Copyright (c) 1987 by Scott Ballantyne.
  5. ;
  6. ;       This program may be freely distributed.  Improvements are welcome,
  7. ;       but any code based on this program can never become proprietary.
  8. ;       You are welcome to use, improve, and distribute this program.
  9. ;       You are forbidden to prevent anyone else from using, improving,
  10. ;       or distributing this program.
  11. ;
  12. ;       Another Contribution to the holy cause of replacing ADOS commands.
  13. ;       This comes in at 588 bytes, using the manx assembler.  As usual,
  14. ;       using metacomposts assembler should shave about 72 bytes away.
  15. ;       The BCPL version is 1092 bytes.
  16. ;       This is a replacement for Protect, which is based on the Un*x
  17. ;       chmod command.
  18. ;       Syntax is:
  19. ;
  20. ;       Chmod [operand] [permissions] file(s)
  21. ;
  22. ;       Operand must be given, and is one of '+' '-' or '='.
  23. ;       Permissions are optional.  If given must follow immediately with
  24. ;       no intervening whitespace.
  25. ;       i.e. +rwed or -red or =rwed.
  26. ;       files are any file names - multiple filenames may be given.  Aside
  27. ;       from convenience, this allows shell programs to expand wild carded
  28. ;       names. You can use spaces as delimiters, or quotes - your choice.
  29. ;
  30. ;       Here is how it works:
  31. ;       + will add the permissions following to the file(s).  No other
  32. ;       permissions are affected.
  33. ;       - will add remove the permissions following from the file(s). No other
  34. ;       permissions are affected
  35. ;       = sets permissions absolutely - only the permissions given in the
  36. ;       command line will be allowed.
  37. ;
  38. ;       Chmod +r file1 file2 file3
  39. ;       Will allow reading for file1 file2 file3 without changing any other
  40. ;       permission.
  41. ;       Chmod -rwe file1 file2 file3
  42. ;       Will disallow reading writing and executing for file1 file2 and file3
  43. ;       without affecting anyother permissions.
  44. ;       Chmod =r file1 file2 file3
  45. ;       File1 file2 and file3 permissions are set absolutely to r(ead) only.
  46. ;       All other permissions are disallowed.
  47. ;
  48. ;       Differences from the UN*X:
  49. ;
  50. ;       Only the symbolic rwed characters are accepted - no octal digits.
  51. ;       Won't accept multiple permissions separated by comma's. I.E.
  52. ;       under un*x you can
  53. ;       Chmod a-x,g-r file(s)
  54. ;       but not in this version.  Didn't seem needed here, since there is
  55. ;       only one owner of a file.  Easily added if turns out to be a need
  56. ;       for it.
  57. ;       Files are pushed on the stack, and so are processed in reverse order
  58. ;       of entry.
  59. ;
  60. LIBRARY_VERSION         equ     31
  61. _AbsExecBase            equ     4
  62.  
  63. _LVOOpenLibrary         equ     -552
  64. _LVOCloseLibrary        equ     -414
  65. _LVOFreeMem             equ     -210
  66. _LVOAllocMem            equ     -198
  67.  
  68. _LVOSetProtection       equ     -186
  69. _LVOExamine             equ     -102
  70. _LVOUnLock              equ     -90
  71. _LVOLock                equ     -84
  72. _LVOOutput              equ     -60
  73. _LVOWrite               equ     -48
  74.  
  75. FIB_SIZE                equ     260     ; size of struct fib
  76. fib_Protection          equ     116     ; offset to protection var
  77.  
  78. EXCLUSIVE_LOCK          equ     -1
  79.  
  80.         entry   Start
  81.         rts
  82. Start:
  83.         move.l  sp,a5           ; save stack pointer.
  84.         move.l  a0,a2           ; move ptr to args
  85.         move.l  #20,d4          ; set error
  86.         move.l  _AbsExecBase,a6
  87.         move.l  #FIB_SIZE,d0    ; get a fib
  88.         moveq.l #0,d1           ; anywhere
  89.         jsr     _LVOAllocMem(a6)
  90.         tst.l   d0
  91.         beq.s   gone
  92.         move.l  d0,a4
  93.         move.l  #LIBRARY_VERSION,d0
  94.         lea     DOSName(pc),a1
  95.         jsr     _LVOOpenLibrary(a6)
  96.         tst.l   d0
  97.         beq.s   nodos
  98.         move.l  d0,a6
  99.         moveq.l #0,d6           ; clear permissions
  100. sbl:    move.b  (a2)+,d7        ; skip blanks
  101.         cmp.b   #' ',d7
  102.         beq.s   sbl
  103.         move.l  #'-+=',d5
  104. oploop:
  105.         cmp.b   d5,d7
  106.         beq.s   getp
  107.         lsr.l   #8,d5
  108.         bne.s   oploop
  109.         move.b  #'=',d7         ; set default operator
  110.         subq.l  #1,a2           ; unget a character
  111. getp:
  112.         move.b  (a2)+,d0
  113.         cmp.b   #$0a,d0
  114.         beq.s   usage
  115.         cmp.b   #' ',d0
  116.         bne.s   lab
  117.         cmp.b   #'=',d7         ; missing permissions only make sense with =
  118.         bne.s   done            ; subtract nothing, add nothing
  119.         move.b  #$00,d6         ; After this is inverted, it will disallow
  120.         bra.s   files           ; all permissions.
  121. lab:
  122.         and.b   #$5f,d0
  123.         moveq.l #0,d3           ; permission bit number
  124.         moveq.l #1,d4           ; permission bit mask
  125.         move.l  #'RWED',d5      ; permission symbols
  126. lab1:
  127.         cmp.b   d5,d0
  128.         beq.s   gotit
  129.         addq.b  #1,d3           ; up the stakes
  130.         lsr.l   #8,d5           ; shift next character in
  131.         bne.s   lab1            ; check next
  132.         bra.s   usage           ; ah oh.
  133. gotit:
  134.         asl.l   d3,d4           ; calc permission
  135.         or.l    d4,d6
  136.         move.b  (a2)+,d0        ; get another character
  137.         cmp.b   #$0a,d0
  138.         beq.s   usage
  139.         cmp.b   #' ',d0
  140.         bne.s   lab
  141. files:
  142.         clr.l   -(sp)           ; push a NULL
  143.         subq.l  #1,a2           ; unget a character
  144. getfiles:
  145.         move.b  #' ',d1
  146. skipbl: move.b  (a2)+,d0
  147.         cmp.b   d1,d0
  148.         beq.s   skipbl
  149.         cmp.b   #$0a,d0
  150.         beq.s   gotfiles
  151.         move.l  a2,-(sp)        ; push pointer on the stack
  152.         cmp.b   #'"',d0
  153.         bne.s   notq
  154.         move.b  d0,d1           ; stuff quote
  155.         bra.s   null
  156. notq:   subq.l  #1,(sp)         ; move pointer back to start of filename
  157. null:   move.b  (a2)+,d0
  158.         cmp.b   #$0a,d0
  159.         beq.s   zap
  160.         cmp.b   d1,d0
  161.         bne.s   null
  162. zap:    clr.b   -1(a2)          ; zap
  163.         cmp.b   #$0a,d0
  164.         bne.s   getfiles        
  165.         ;
  166.         ; Got the files, now get on with the show
  167.         ;
  168. gotfiles:
  169.         tst.l   (sp)            ; Did we get a filename?
  170.         beq.s   usage           ; no files....
  171.         cmp.b   #'-',d7         ; need to flip bits?
  172.         beq.s   main
  173.         eor.b   #$0f,d6         ; + = need to be inverted.
  174. main:
  175.         move.l  (sp)+,a3        ; get pointer to filename
  176.         cmp.l   #0,a3
  177.         beq.s   done
  178.         cmp.b   #'=',d7         ; is it absolute?
  179.         bne.s   longway         ; go longway around
  180.         move.l  d6,d2
  181.         bra.s   dothedeed
  182.         ;
  183.         ; get current permission
  184.         ;
  185. longway:
  186.         move.l  a3,d1           ; filename
  187.         move.l  #EXCLUSIVE_LOCK,d2
  188.         jsr     _LVOLock(a6)
  189.         tst.l   d0
  190.         beq.s   warn            ; warning, then back to main
  191. ok:     move.l  d0,d5           ; save lock so we can free later
  192.         move.l  d0,d1
  193.         move.l  a4,d2           ; fib pointer
  194.         jsr     _LVOExamine(a6) ; file in fib
  195.         move.l  d0,d2           ; save return code
  196.         move.l  d5,d1
  197.         jsr     _LVOUnLock(a6)
  198.         tst.l   d0
  199.         beq.s   warn            ; warning, then back to main
  200. okok:   move.l  fib_Protection(a4),d2
  201.         cmp.b   #'+',d7         ; set or clear dem bits
  202.         bne.s   orem
  203.         and.b   d6,d2
  204.         bra.s   dothedeed
  205. orem:   or.b    d6,d2
  206. dothedeed:
  207.         move.l  a3,d1           ; filename
  208.         jsr     _LVOSetProtection(a6)
  209.         tst.l   d0
  210.         bne.s   main            ; if ok, loop, otherwise, fall through to
  211.         ;
  212.         ; print warning message, and loop back to main.
  213.         ;
  214. warn:   lea     warning(pc),a0
  215.         bsr.s   writeout
  216.         move.l  a3,a0           ; current filename to a0
  217.         bsr.s   writeout        ; write filename
  218.         lea     lf(pc),a0
  219.         bsr.s   writeout        ; linefeed and return to caller
  220.         bra.s   main            ; and loop back to main
  221.         ;
  222.         ; Some body did something wrong, so give them a usage message.
  223.         ;
  224. usage:  lea     usemsg(pc),a0
  225.         bsr.s   writeout
  226.         moveq.l #0,d4           ; set return code and fall through to done.
  227.         ;
  228.         ; done - enter with d4 = return code.
  229.         ; Deallocate resources, and exit.
  230.         ;
  231. done:   ; Enter here if got FIB and Doslib ok.
  232.         ;
  233.         move.l  a6,a1
  234.         move.l  _AbsExecBase,a6
  235.         jsr     _LVOCloseLibrary(a6)
  236. nodos:  ;
  237.         ; Enter here if FIB but no dos
  238.         move.l  a4,a1
  239.         move.l  #FIB_SIZE,d0
  240.         jsr     _LVOFreeMem(a6)
  241.         ;
  242.         ; Enter here if everything went wrong
  243.         ;
  244. gone:   move.l  a5,sp           ; restore stack pointer
  245.         move.l  d4,d0           ; set return code
  246.         rts
  247.         ;
  248.         ;
  249.         ; enter with pointer to null terminated string in A0
  250.         ; Trashes d0-d3/a0
  251. writeout:
  252.         move.l  a0,d2           ; set string pointer for Write()
  253.         move.l  #-1,d3
  254. cnt:    addq.l  #1,d3
  255.         tst.b   (a0)+
  256.         bne.s   cnt     
  257.         jsr     _LVOOutput(a6)
  258.         move.l  d0,d1
  259.         jmp     _LVOWrite(a6)
  260.  
  261. usemsg:         dc.b    "Usage: chmod [=] [+] [-] [r][w][e][d] file(s)",10,0
  262. warning:        dc.b    "Can't change permission(s) for '",0
  263. lf:             dc.b    "'.",10,0
  264. DOSName:        dc.b    "dos.library",0
  265.